<?php

namespace App\Util\Redis;

use App\Enum\ErrorCodeEnum;
use Exception;

/**
 * 通用 api 错误异常
 */
class RedisLockException extends Exception
{
    const DEFAULT_MSG = '系统繁忙！';
    const FIELD_INTERNAL_ERROR = 'internalError';

    protected $code;
    protected $context = [];
    protected $data = [];
    protected $internalError = '';

    public function __construct($message, $context = [], $data = [], $code = null)
    {
        $code = $code ?: ErrorCodeEnum::FAIL()->getKey();
        // 内部错误
        $message = $internalError = $message ?: self::DEFAULT_MSG;
        if (isset($context['internalError'])) {
            $internalError = $context['internalError'];
            unset($context['internalError']);
        }
        // 显示详情
        if (!empty($context['showDetail'])) {
            $context['exception'] = $this;
            unset($context['showDetail']);
        }

        $this->code = $code;
        $this->context = $context;
        $this->data = $data;
        $this->internalError = $internalError;
        parent::__construct($message, $code);
    }

    public function getContext()
    {
        return $this->context;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getInternalError()
    {
        return $this->internalError;
    }
}
